//
//  JWBJSiftView.m
//  jinwang
//
//  Created by jzb on 2018/3/19.
//  Copyright © 2018年 jzb. All rights reserved.
//

#import "JWBJSiftView.h"
#import "SFDualWaySlider.h"//双向滑块

@interface JWBJSiftView ()<UIPickerViewDelegate,UIPickerViewDataSource>
{
    
    CGFloat AddressHeight;
    UITableView *SiftTV;
    
    NSString *_homeAddStr;
    NSString *_homeAddStrID;
    NSString *string1;
    NSString *string2;
    NSString *string3;
    
    UIView *_AddressSeconView;
    
    SFDualWaySlider *slider;
    NSMutableDictionary *_SiftDic;
    
    NSString *user_sex;//性别0男1女2不限
    NSString *user_active;//活跃0一小时1六小时2今日3昨日4一周
    NSString *region_id;//区域
    NSString *user_age;// 年龄
    NSString *user_hobby;//爱好
    NSString *user_occupation;//职业
    NSString *user_type;//0家门1老乡
    NSString *user_rank;//只看会员0否1是
}


@property (nonatomic,strong) NSArray *addressArr; // 解析出来的最外层数组
@property (nonatomic,strong) NSArray *provinceArr; // 省
@property (nonatomic,strong) NSArray *countryArr; // 市
@property (nonatomic,strong) NSArray *districtArr; // 区
@property (nonatomic,strong) NSArray *districtIDArr; // 区
@property (nonatomic,assign) NSInteger index1; // 省下标
@property (nonatomic,assign) NSInteger index2; // 市下标
@property (nonatomic,assign) NSInteger index3; // 区下标

@end
@implementation JWBJSiftView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {

        [self TableViewBuild];
        [self AddressJsonRequest];
        
    }
    return self;
}
- (void)TableViewBuild{
    
    self.backgroundColor = [UIColor cyanColor];
    
    user_sex = @"2";
    user_active = @"4";
    region_id = @"0";
    user_age = @"0,100";
    user_hobby = @"";
    user_occupation = @"";
    user_type = @"0";
    user_rank = @"0";
    
    SiftTV = [[UITableView alloc]initWithFrame:CGRM(0, 0, 750-80, 1334-340) style:UITableViewStylePlain];
    
    SiftTV.delegate = self;
    SiftTV.dataSource = self;
    SiftTV.separatorStyle = UITableViewCellSeparatorStyleNone;
    [SiftTV registerNib:[UINib nibWithNibName:@"JWBJSiftViewCell" bundle:nil] forCellReuseIdentifier:@"JWBJSiftViewCell"];
    [self addSubview:SiftTV];
    
    slider = [[SFDualWaySlider alloc] initWithFrame:CGRM(0, 0, 750-80-16*4, 29*2) minValue:1 maxValue:100 blockSpaceValue:0];
    slider.progressRadius = 2.5;
    //    [slider.minIndicateView setTitle:@"不限"];
    //    [slider.maxIndicateView setTitle:@"不限"];
    //    slider.lightColor = [UIColor yellowColor];
    slider.minIndicateView.backIndicateColor = UIColorHex(0xEEA72B);
    slider.maxIndicateView.backIndicateColor = [UIColor greenColor];
    slider.lightColor = UIColorHex(0xEEA72B);
    slider.minIndicateView.hidden = YES;
    slider.maxIndicateView.hidden= YES;
    slider.blockImage = [UIImage imageNamed:@"jw_bj_sliderIcon"];
    
    slider.currentMinValue = 1;
    slider.currentMaxValue = 100;
    //分段 表示前部分占比80%  所在值范围为[0,30]  即剩下的 20%滑动距离 值范围为[30，80]
    slider.frontScale = 1;
    slider.frontValue = 100;
    slider.spaceInBlocks = 1;
    slider.progressHeight = 5;
    
    slider.indicateViewOffset = 10;
    slider.indicateViewWidth = 40;
}
#pragma mark -UITableViewDelegate,UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    self.cell = [tableView dequeueReusableCellWithIdentifier:[JWBJSiftViewCell getCellReuseIdentifier]];
    self.cell.contentView.backgroundColor = [UIColor clearColor];
    
    [self.cell.SiftAllSexBtn addTarget:self action:@selector(cellButtonOnclikc:) forControlEvents:UIControlEventTouchUpInside];
    [self.cell.SiftMenSexBtn addTarget:self action:@selector(cellButtonOnclikc:) forControlEvents:UIControlEventTouchUpInside];
    [self.cell.SiftWomenSexBtn addTarget:self action:@selector(cellButtonOnclikc:) forControlEvents:UIControlEventTouchUpInside];
    [self.cell.SiftTimeOneHourAgoBtn addTarget:self action:@selector(cellButtonOnclikc:) forControlEvents:UIControlEventTouchUpInside];
    [self.cell.SiftTimeSixHourAgoBtn addTarget:self action:@selector(cellButtonOnclikc:) forControlEvents:UIControlEventTouchUpInside];
    [self.cell.SiftTimeTodayBtn addTarget:self action:@selector(cellButtonOnclikc:) forControlEvents:UIControlEventTouchUpInside];
    [self.cell.SiftTimeYesTodayBtn addTarget:self action:@selector(cellButtonOnclikc:) forControlEvents:UIControlEventTouchUpInside];
    [self.cell.SiftTimeOneWeekBtn addTarget:self action:@selector(cellButtonOnclikc:) forControlEvents:UIControlEventTouchUpInside];
    [self.cell.AddressChoseBtn addTarget:self action:@selector(cellButtonOnclikc:) forControlEvents:UIControlEventTouchUpInside];
    [self.cell.AddressQueDingBtn addTarget:self action:@selector(cellButtonOnclikc:) forControlEvents:UIControlEventTouchUpInside];
    [self.cell.AddressQuXiaoBtn addTarget:self action:@selector(cellButtonOnclikc:) forControlEvents:UIControlEventTouchUpInside];
    [self.cell.SiftHomeBtn addTarget:self action:@selector(cellButtonOnclikc:) forControlEvents:UIControlEventTouchUpInside];
    [self.cell.SiftLaoXiangBtn addTarget:self action:@selector(cellButtonOnclikc:) forControlEvents:UIControlEventTouchUpInside];
    [self.cell.QuXiaoBtn addTarget:self action:@selector(cellButtonOnclikc:) forControlEvents:UIControlEventTouchUpInside];
    [self.cell.QueRenBtn addTarget:self action:@selector(cellButtonOnclikc:) forControlEvents:UIControlEventTouchUpInside];
    [self.cell.SiftSwitch addTarget:self action:@selector(SiftSwitchOnclick:) forControlEvents:UIControlEventValueChanged];
    
//    [self.cell.AgeSlider addTarget:self action:@selector(sliderValueChanged:) forControlEvents:UIControlEventValueChanged];// 针对值变化添加响应方法
    
    
    [self.cell.AgeSliderBGView addSubview:slider];
    __weak __typeof(self) weakSelf = self;
    slider.sliderValueChanged = ^(CGFloat minValue, CGFloat maxValue) {
        
        weakSelf.cell.AgeLabel.text = [NSString stringWithFormat:@"%.f-%.f",minValue,maxValue];
        user_age = [NSString stringWithFormat:@"%.f,%.f",minValue,maxValue];
    };
    
    //设置标题，如果需要设置默认值 最好先写这个，否则设置默认值后不会第一时间触发
    slider.getMinTitle = ^NSString *(CGFloat minValue) {
        if (floor(minValue) == 0.f) {
            return @"不限";
        }else{
            return [NSString stringWithFormat:@"%.fK",floor(minValue)];
        }
        
    };
    
    slider.getMaxTitle = ^NSString *(CGFloat maxValue) {
        if (floor(maxValue) == 100.f) {
            return @"不限";
        }else{
            return [NSString stringWithFormat:@"%.fK",floor(maxValue)];
        }
    };
    

    
    return self.cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 490 + AddressHeight;
}
- (void)cellButtonOnclikc:(UIButton *)sender{
    
    if (sender.tag == 1000 || sender.tag == 1001 || sender.tag == 1002) {
        UIButton *btn01 = (UIButton *)[self viewWithTag:1000];
        UIButton *btn02 = (UIButton *)[self viewWithTag:1001];
        UIButton *btn03 = (UIButton *)[self viewWithTag:1002];
        [btn01 setTitleColor:UIColorHex(0x969696) forState:UIControlStateNormal];
        [btn02 setTitleColor:UIColorHex(0x969696) forState:UIControlStateNormal];
        [btn03 setTitleColor:UIColorHex(0x969696) forState:UIControlStateNormal];
        [btn02 setImage:[UIImage imageNamed:@"jw_bj_manIcon"] forState:UIControlStateNormal];
        [btn03 setImage:[UIImage imageNamed:@"jw_bj_WomanIcon"] forState:UIControlStateNormal];
        if (sender.tag == 1000) {
            [btn01 setTitleColor:UIColorHex(0xEEA72B) forState:UIControlStateNormal];
            
            user_sex = @"2";
        }else if (sender.tag == 1001){
            [btn02 setTitleColor:UIColorHex(0xEEA72B) forState:UIControlStateNormal];
            [btn02 setImage:[UIImage imageNamed:@"jw_bj_manIcon_sel"] forState:UIControlStateNormal];
            user_sex = @"0";
        }else{
            [btn03 setTitleColor:UIColorHex(0xEEA72B) forState:UIControlStateNormal];
            [btn03 setImage:[UIImage imageNamed:@"jw_bj_WomanIcon_sel"] forState:UIControlStateNormal];
            user_sex = @"1";
        }
    }else if (sender.tag == 2000 || sender.tag == 2001 || sender.tag == 2002 || sender.tag == 2003 || sender.tag == 2004){
        UIButton *btn01 = (UIButton *)[self viewWithTag:2000];
        UIButton *btn02 = (UIButton *)[self viewWithTag:2001];
        UIButton *btn03 = (UIButton *)[self viewWithTag:2002];
        UIButton *btn04 = (UIButton *)[self viewWithTag:2003];
        UIButton *btn05 = (UIButton *)[self viewWithTag:2004];
        [btn01 setTitleColor:UIColorHex(0x969696) forState:UIControlStateNormal];
        [btn02 setTitleColor:UIColorHex(0x969696) forState:UIControlStateNormal];
        [btn03 setTitleColor:UIColorHex(0x969696) forState:UIControlStateNormal];
        [btn04 setTitleColor:UIColorHex(0x969696) forState:UIControlStateNormal];
        [btn05 setTitleColor:UIColorHex(0x969696) forState:UIControlStateNormal];
        
        if (sender.tag == 2000) {
            [btn01 setTitleColor:UIColorHex(0xEEA72B) forState:UIControlStateNormal];
            user_active = @"0";
        }else if (sender.tag == 2001){
            [btn02 setTitleColor:UIColorHex(0xEEA72B) forState:UIControlStateNormal];
            user_active = @"1";
        }else if (sender.tag == 2002){
            [btn03 setTitleColor:UIColorHex(0xEEA72B) forState:UIControlStateNormal];
            user_active = @"2";
        }else if (sender.tag == 2003){
            [btn04 setTitleColor:UIColorHex(0xEEA72B) forState:UIControlStateNormal];
            user_active = @"3";
        }else{
            [btn05 setTitleColor:UIColorHex(0xEEA72B) forState:UIControlStateNormal];
            user_active = @"4";
        }
        
    }else if (sender.tag == 3000 || sender.tag == 3001 || sender.tag == 3002){
        if (sender.tag == 3000) {
            self.cell.AddressBGView.hidden = NO;
            self.cell.AddressBGViewHeight.constant = 200;
            AddressHeight = 200;
            [self.cell.AddressBGView addSubview:_AddressSeconView];
            [SiftTV reloadData];
        }else if (sender.tag == 3001){
            self.cell.AddressBGView.hidden = YES;
            self.cell.AddressBGViewHeight.constant = 0;
            AddressHeight = 0;
            [SiftTV reloadData];
            
        }else{
            self.cell.AddressBGView.hidden = YES;
            self.cell.AddressBGViewHeight.constant = 0;
            AddressHeight = 0;
            [SiftTV reloadData];
            [self.cell.AddressChoseBtn setTitle:_homeAddStr forState:UIControlStateNormal];
            [self.cell.AddressChoseBtn setTitleColor:UIColorHex(0xEEA72B) forState:UIControlStateNormal];
            region_id = _homeAddStrID;
        }
    }else if (sender.tag == 6000 || sender.tag == 6001) {
        UIButton *btn01 = (UIButton *)[self viewWithTag:6000];
        UIButton *btn02 = (UIButton *)[self viewWithTag:6001];
       
        [btn01 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [btn02 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [btn01 setImage:[UIImage imageNamed:@"jw_grayYuan"] forState:UIControlStateNormal];
        [btn02 setImage:[UIImage imageNamed:@"jw_grayYuan"] forState:UIControlStateNormal];
        if (sender.tag == 6000) {
            [btn01 setTitleColor:UIColorHex(0xEEA72B) forState:UIControlStateNormal];
            [btn01 setImage:[UIImage imageNamed:@"jw_sex_btn_sel"] forState:UIControlStateNormal];
            
            user_type = @"0";
           
        }else if (sender.tag == 6001){
            [btn02 setTitleColor:UIColorHex(0xEEA72B) forState:UIControlStateNormal];
            [btn02 setImage:[UIImage imageNamed:@"jw_sex_btn_sel"] forState:UIControlStateNormal];
            user_type = @"1";
        }
    }else if (sender.tag == 8000 || sender.tag == 8001) {
        
        if (sender.tag == 8000) {
            
            self.siftDateBlcok(NO, user_age, user_active, region_id, user_age, user_hobby, user_occupation, user_type, user_rank);

        }else if (sender.tag == 8001){
            
            user_hobby = self.cell.HobTF.text;
            user_occupation = self.cell.WorkTF.text;
            
            self.siftDateBlcok(YES,user_sex, user_active, region_id, user_age, user_hobby, user_occupation, user_type, user_rank);
        }
    }
    
}
- (void)SiftSwitchOnclick:(id)sender{
     UISwitch *switchButton = (UISwitch*)sender;
    BOOL isButtonOn = [switchButton isOn];
    NSLog(@"-=-=>>%@",isButtonOn?@"YES":@"NO");
    if (isButtonOn) {
        user_rank = @"1";
    }else {
        user_rank = @"0";
    }
}
// slider变动时改变label值
- (void)sliderValueChanged:(id)sender {
    UISlider *slider = (UISlider *)sender;
    self.cell.AgeLabel.text = [NSString stringWithFormat:@"0-%.f", slider.value];
}
- (NSArray *)provinceArr
{
    if (_provinceArr == nil) {
        _provinceArr = [[NSArray alloc] init];
    }
    return _provinceArr;
}
-(NSArray *)countryArr
{
    if(_countryArr == nil)
    {
        _countryArr = [[NSArray alloc] init];
    }
    return _countryArr;
}

- (NSArray *)districtArr
{
    if (_districtArr == nil) {
        _districtArr = [[NSArray alloc] init];
    }
    return _districtArr;
}

-(NSArray *)addressArr
{
    if (_addressArr == nil) {
        _addressArr = [[NSArray alloc] init];
    }
    return _addressArr;
}
- (void)AddressPickerViewBuild{
    
    _AddressSeconView = [[UIView alloc]initWithFrame:CGRM(0, 60, 750-80-60, 340)];
    UIPickerView *HomeAddpickView = [[UIPickerView alloc]initWithFrame:CGRM(0, 0, 750-80-60, 340)];
    
    [HomeAddpickView selectRow:0 inComponent:0 animated:YES];
    
    HomeAddpickView.delegate = self;
    HomeAddpickView.dataSource = self;
//        HomeAddpickView.backgroundColor = [UIColor purpleColor];
    
    [_AddressSeconView addSubview:HomeAddpickView];
}
- (void)loadFirstData
{
    
    NSMutableArray *firstName = [[NSMutableArray alloc] init];
    for (NSDictionary *dict in self.addressArr)
    {
        NSString *name = [dict objectForKey:@"short_name"];
        [firstName addObject:name];
    }
    // 第一层是省份 分解出整个省份数组
    self.provinceArr = firstName;
}
// 根据传进来的下标数组计算对应的三个数组
- (void)calculateFirstData
{
    // 拿出省的数组
    [self loadFirstData];
    
    NSMutableArray *cityNameArr = [[NSMutableArray alloc] init];
    // 根据省的index1，默认是0，拿出对应省下面的市
    NSDictionary *cityDic = self.addressArr[self.index1];
    NSArray *cityArr = [cityDic objectForKey:@"children"];
    for (NSDictionary *cityName in cityArr) {
        
        NSString *name1 = [cityName objectForKey:@"short_name"];
        [cityNameArr addObject:name1];
    }
    // 组装对应省下面的市
    NSMutableArray *districtNameArr = [[NSMutableArray alloc] init];
    NSMutableArray *districtIDArr = [[NSMutableArray alloc] init];
    self.countryArr = cityNameArr;
    NSDictionary *districtDic = cityArr[self.index2];
    NSArray *districtArr001 = [districtDic objectForKey:@"children"];
    for (NSDictionary *istrictName in districtArr001) {
        
        NSString *name1 = [istrictName objectForKey:@"short_name"];
        NSString *districtID = [NSString stringWithFormat:@"%@",[istrictName objectForKey:@"region_id"]];
        [districtNameArr addObject:name1];
        [districtIDArr addObject:districtID];
    }
    //                             index1对应省的字典         市的数组 index2市的字典   对应市的数组
    // 这里的allValue是取出来的大数组，取第0个就是需要的内容
    self.districtArr = districtNameArr;
    self.districtIDArr = districtIDArr;
}
#pragma mark - UIPickerViewDataSource Implementation
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 3;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    // Returns
    switch (component)
    {
        case 0: return self.provinceArr.count;
        case 1: return self.countryArr.count;
        case 2:return self.districtArr.count;
        default:break;
    }
    return 0;
}
#pragma mark UIPickerViewDelegate Implementation


- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    switch (component)
    {
        case 0: return self.provinceArr[row];break;
        case 1: return self.countryArr[row];break;
        case 2:return self.districtArr[row];break;
        default:break;
    }
    return nil;
}
- (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component{
    
    NSString *titleString;
    
    if (component == 0) {
        
        titleString = self.provinceArr[row];
    }
    else if (component == 1){
        
        titleString = self.countryArr[row];
        
    }
    else if (component == 2){
        
        titleString = self.districtArr[row];
        
    }
    
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:titleString];
    NSRange range = [titleString rangeOfString:titleString];
    [attributedString addAttribute:NSForegroundColorAttributeName value:UIColorHex(0xEEA72B) range:range];
    
    return attributedString;
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    //设置分割线的颜色
    for(UIView *singleLine in pickerView.subviews)
    {
        if (singleLine.frame.size.height < 1)
        {
            singleLine.backgroundColor = UIColorHex(0xEEA72B);
        }
    }
    UILabel* label = (UILabel*)view;
    if (!label)
    {
        label = [[UILabel alloc] init];
        [label setFont:[UIFont systemFontOfSize:17]];
        label.textColor = kRGBColor(80, 80, 80);
        label.adjustsFontSizeToFitWidth = YES;
        [label setTextAlignment:NSTextAlignmentCenter];
        [label setBackgroundColor:[UIColor clearColor]];
    }
    
    NSString * title = @"";
    switch (component)
    {
        case 0: title =   self.provinceArr[row];break;
        case 1: title =   self.countryArr[row];break;
        case 2: title =   self.districtArr[row];break;
        default:break;
    }
    label.textAlignment = NSTextAlignmentCenter;
    label.text=title;
    
    if(component == 0){
        if(row == _index1){
            label.attributedText
            = [self pickerView:pickerView attributedTitleForRow:_index1 forComponent:component];
        }else{
            label.text = [self pickerView:pickerView titleForRow:row forComponent:component];
        }
    }else if (component == 1){
        if(row == _index2){
            label.attributedText
            = [self pickerView:pickerView attributedTitleForRow:_index2 forComponent:component];
        }else{
            label.text = [self pickerView:pickerView titleForRow:row forComponent:component];
        }
    }else if (component == 2){
        if(row == _index3){
            label.attributedText
            = [self pickerView:pickerView attributedTitleForRow:_index3 forComponent:component];
        }else{
            
            label.text = [self pickerView:pickerView titleForRow:row forComponent:component];
        }
    }
    
    return label;
}
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
    
    return 40;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    UILabel *piketLabel =  (UILabel *)[pickerView viewForRow:row forComponent:component];
    piketLabel.textColor = UIColorHex(0xEEA72B);
    
    switch (component)
    {
        case 0:
        {
            self.index1 = row;
            self.index2 = 0;
            self.index3 = 0;
            //            [self calculateData];
            // 滚动的时候都要进行一次数组的刷新
            [self calculateFirstData];
            [pickerView reloadComponent:1];
            [pickerView reloadComponent:2];
            [pickerView selectRow:0 inComponent:1 animated:YES];
            [pickerView selectRow:0 inComponent:2 animated:YES];
            string1 = self.provinceArr[row];
            string2 = self.countryArr[self.index2];
            string3 = self.districtArr[self.index3];
            _homeAddStrID = self.districtIDArr[self.index3];
        }
            break;
            
        case 1:
        {
            self.index2 = row;
            self.index3 = 0;
            //            [self calculateData];
            [self calculateFirstData];
            [pickerView selectRow:0 inComponent:2 animated:YES];
            [pickerView reloadComponent:2];
            string2 = self.countryArr[row];
            string3 = self.districtArr[self.index3];
            _homeAddStrID = self.districtIDArr[self.index3];
        }
            break;
        case 2:
            self.index3 = row;
            string3 = self.districtArr[row];
            _homeAddStrID = self.districtIDArr[self.index3];
            break;
        default:break;
    }
    
    _homeAddStr = [NSString stringWithFormat:@"%@-%@-%@",string1,string2,string3];
    
//    _user_addressLabel.text = _homeAddStr;
//    _user_addressLabel.textColor = [UIColor darkGrayColor];
    
}
- (void)configurePickerView:(UIPickerView *)pickerView
{
    pickerView.showsSelectionIndicator = YES;
}

- (void)AddressJsonRequest{
    
    [[DDNetworkManagerDate makeUrl_sendsms_with_base_allRegion] post_RequestFinshSuccess:^(id responseObject) {
        NSLog(@"====>>>%@",responseObject);
//        if ([self requestFinshCode:responseObject isShowMessage:YES] == 1) {
        
            NSMutableArray *dataArr = [responseObject objectForKey:@"data"];
            
            if (dataArr.count > 0 && dataArr) {
                
                self.addressArr = dataArr;
                [self AddressPickerViewBuild];
                [self calculateFirstData];
                [SiftTV reloadData];
                
//                [self HomeAddressBuild];
            }
            
//        }
    } failure:^(id errorObject) {
    }];
    
}
@end
